/**
Zeke Elder - zbe@andrew.cmu.edu
Amal Chemmany - achemman@andrew.cmu.edu
Nishant Kumar - nkumar1@andrew.cmu.edu
15-637
4/2/2012
 **/

package edu.cmu.fithub.actions;

import java.sql.Blob;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import edu.cmu.fithub.controller.Action;
import edu.cmu.fithub.db.DBHelper;
import edu.cmu.fithub.db.bean.User;
import edu.cmu.fithub.formbeans.LoginForm;
import edu.cmu.fithub.formbeans.RegisterForm;
import edu.cmu.fithub.formbeans.UpdateProfileForm;

public class UpdateProfileAction extends Action {

	private FormBeanFactory<UpdateProfileForm> formFactory = FormBeanFactory
			.getInstance(UpdateProfileForm.class);

	public UpdateProfileAction() {
	}

	public String getName() {
		return "updateProfile.do";
	}

	public String perform(HttpServletRequest request) {

		List<String> errors = new ArrayList<String>();
		request.getSession().setAttribute("errors", errors);

		try {
			UpdateProfileForm form = formFactory.create(request);
			request.setAttribute("form", form);

			if (!form.isPresent()) {
				return "showHome.do";
			}

			errors.addAll(form.getValidationErrors());

			Session dbSession = DBHelper.getSessionFactory().openSession();
			User user = (User) request.getSession().getAttribute("user");
			if (errors.size() == 0 && user != null) {
				if (form.getFirstName() != null
						&& !form.getFirstName().isEmpty()) {
					user.setFirstName(form.getFirstName());
				}

				if (form.getLastName() != null && !form.getLastName().isEmpty()) {
					user.setLastName(form.getLastName());
				}

				if (form.getPassword() != null && !form.getPassword().isEmpty()) {
					user.setPassword(form.getPassword());
				}

				if (form.getTimeZone() != null && !form.getTimeZone().isEmpty()) {
					user.setTimeZone(form.getTimeZone());
				}

				if (form.getAge() != null && !form.getAge().isEmpty()) {
					user.setAge(Integer.parseInt(form.getAge()));
				}

				if (form.getEmail() != null && !form.getEmail().isEmpty()) {
					user.setEmail(form.getEmail());
				}

				if (form.getWeight() != null && !form.getWeight().isEmpty()) {
					user.setWeight(Integer.parseInt(form.getWeight()));
				}

				if (form.getImage() != null
						&& form.getImage().getBytes() != null
						&& form.getImage().getBytes().length != 0) {
					Blob imageBlob = Hibernate.getLobCreator(dbSession)
							.createBlob(form.getImage().getBytes());
					user.setImage(imageBlob);
					user.setHasImage(true);
				}
			}

			if (errors.size() > 0) {
				return "updateProfile.jsp";
			} else {
				dbSession.beginTransaction();
				dbSession.update(user);
				dbSession.getTransaction().commit();
			}

			return "redirectToHome.jsp";

		} catch (FormBeanException e) {
			errors.add(e.toString());
			return "redirectToHome.jsp";
		}
	}
}
